package com.uchiha.nova.mainService.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uchiha.nova.core.model.dto.DictDTO;
import com.uchiha.nova.core.model.entity.DictEntity;
import com.uchiha.nova.core.model.query.DictQuery;
import com.uchiha.nova.core.model.vo.DictVO;
import com.uchiha.nova.core.tool.api.R;
import com.uchiha.nova.core.tool.constant.NovaCRUDMethodConstant;
import com.uchiha.nova.core.tool.support.BaseController;
import com.uchiha.nova.core.tool.support.Condition;
import com.uchiha.nova.core.tool.support.QueryContext;
import com.uchiha.nova.core.tool.util.Func;
import com.uchiha.nova.mainService.cache.DictCache;
import com.uchiha.nova.mainService.service.IDictService;
import com.uchiha.nova.mainService.wrapper.DictWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 系统字典 控制器
 *
 * @author Uchihashiamu
 */
@RestController
@AllArgsConstructor
@RequestMapping("/dict")
@Tag(name = "系统字典")
@Slf4j
public class DictController extends BaseController {

    @Resource
    IDictService dictService;

    /**
     * 详情
     */
    @PostMapping(NovaCRUDMethodConstant.NOVA_WEB_GETONE)
    @Operation(summary = "详情")
    public R<DictVO> getOne(@RequestBody DictDTO dictDTO) {
        DictEntity entity = new DictEntity();
        BeanUtil.copyProperties(dictDTO, entity);
        DictEntity detail = dictService.getOne(Condition.getQueryWrapper(entity));
        return R.data(DictWrapper.build().entityVO(detail));
    }

    /**
     * 分页 代码自定义代号
     */
    @GetMapping(NovaCRUDMethodConstant.NOVA_WEB_PAGE)
    @Operation(summary = "分页查询")
    public R getPage(DictQuery query, QueryContext queryContext) {
        IPage<DictEntity> pages = dictService.page(Condition.getPage(queryContext), Condition.getQueryWrapper(query, queryContext, DictEntity.class));
        return R.data(DictWrapper.build().pageVO(pages));
    }

    /**
     * 新增 代码自定义代号
     */
    @PostMapping(NovaCRUDMethodConstant.NOVA_WEB_SAVE)
    @Operation(summary = "新增")
    public R save(@RequestBody DictDTO dictDTO) {
        DictEntity entity = new DictEntity();
        BeanUtil.copyProperties(dictDTO, entity);
        return dictService.submit(entity);
    }

    /**
     * 修改 代码自定义代号
     */
    @PostMapping(NovaCRUDMethodConstant.NOVA_WEB_UPDATE)
    @Operation(summary = "修改")
    public R update(@RequestBody DictDTO dictDTO) {
        DictEntity entity = new DictEntity();
        BeanUtil.copyProperties(dictDTO, entity);
        return dictService.submit(entity);
    }

    /**
     * 新增或修改 代码自定义代号
     */
    @PostMapping(NovaCRUDMethodConstant.NOVA_WEB_SAVEORUPDATE)
    @Operation(summary = "新增或修改")
    public R saveOrUpdate(@RequestBody DictDTO dictDTO) {
        DictEntity entity = new DictEntity();
        BeanUtil.copyProperties(dictDTO, entity);
        return R.status(dictService.saveOrUpdate(entity));
    }


    /**
     * 删除 代码自定义代号
     */
    @PostMapping(NovaCRUDMethodConstant.NOVA_WEB_REMOVE)
    @Operation(summary = "逻辑删除")
    public R remove(@RequestParam String ids) {
        return R.status(dictService.deleteLogic(Func.toLongList(ids)));
    }

    /**
     * 物理删除，不可恢复
     */
    @Operation(summary = "物理删除")
    @DeleteMapping(NovaCRUDMethodConstant.NOVA_WEB_DELETE)
    public R delete(@RequestParam String ids) {
        return R.status(dictService.deletePhysics(Func.toLongList(ids)));
    }


    @Operation(summary = "根据code获得系统字典")
    @GetMapping("/code")
    public R Code(String code) {
        return R.data(DictCache.getList(code));
    }

    @Operation(summary = "获取系统字典父级列表")
    @GetMapping("/child-list")
    public R<List<DictVO>> childList(DictDTO dictDTO){
        DictEntity dict = new DictEntity();
        BeanUtil.copyProperties(dictDTO, dict);
        return R.data(dictService.childList(dict));
    }
}
